package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.AdminService;
import com.tarena.lbs.common.basic.converter.AdminConverter;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.dos.AdminDO;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "admin",tags = "后台管理账号")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @GetMapping("/admin/basic/role/detail")
    @ApiOperation(value = "获取当前登录账号信息")
    public Result<AdminVO> adminDetail() throws BusinessException {
        AdminVO adminVO = adminService.adminDeatil();
        return new Result<>(adminVO);
    }

    @Autowired
    private AdminConverter adminConverter;
    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> adminList(@RequestBody AdminQuery adminQuery) throws BusinessException{
        //封装一恶搞分页 需要4个属性pageNo pageSize<-query
        //total总数 objects 分页数据 需要业务查询 BasePageList
        BasePageList<AdminBO> adminBOS = adminService.adminList(adminQuery);
        PageResult<AdminVO> adminVOPageResult = adminConverter.assemblePageVOS(adminBOS, adminQuery);
        return new Result<>(adminVOPageResult);
    }

    @PostMapping("admin/basic/role/add")
    public Result<Void> adminSave(@RequestBody AdminParam adminParam) throws BusinessException{
        AdminBO adminBO = new AdminBO();
        BeanUtils.copyProperties(adminParam,adminBO);
        adminService.adminSave(adminBO);
        return Result.success();
    }
}
